//
//  MVC_TableViewCell.m
//  MVC|MVP|MVVM
//
//  Created by Guo Rui on 2017/11/13.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "MVC_TableViewCell.h"
#import <UIImageView+WebCache.h>
#import "TimelineModel.h"

@interface MVC_TableViewCell()
@property (weak, nonatomic) IBOutlet UIButton *comentButton;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIImageView *albumView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_height_constraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_width_constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageView_top_constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentLabel_top_constraint;

@property (weak, nonatomic) id<MVCTableViewDataProtocol> model;
@end

@implementation MVC_TableViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.likeButton.contentEdgeInsets =
    self.comentButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);
}

- (IBAction)likeButtonDidClick:(id)sender {
    if (self.likeDidClick) {
        self.likeDidClick();
    }
}

- (IBAction)comentButtonDidClick:(id)sender {
    
}

- (void)loadData:(id<MVCTableViewDataProtocol>)model
{
    _model = model;
    [self update];
}

- (void)update
{
    // 1. albumn的展示逻辑
    MediaModel *mediaModel = [self.model mediaModel];
    if (mediaModel) {
        [self.albumView sd_setImageWithURL:[NSURL URLWithString:mediaModel.imgUrl] placeholderImage:[UIImage imageNamed:@"0"]];
    }
    else {
        self.albumView.image = nil;
    }
    
    // 2. 发布消息
    self.infoLabel.text = [self.model publishInfo];
    
    // 3. 按钮状态
    self.likeButton.selected = [self.model isLike];
    [self.likeButton setTitle:[NSString stringWithFormat:@"%zd", self.model.likeCount] forState:UIControlStateNormal];
    [self.likeButton setTitle:[NSString stringWithFormat:@"%zd", self.model.likeCount] forState:UIControlStateSelected];
    [self.comentButton setTitle:[NSString stringWithFormat:@"%zd", self.model.commentCount] forState:UIControlStateNormal];
    
    // 4. 内容
    if (self.model.timelineContent) {
        self.contentLabel.text = self.model.timelineContent;
    }
    else {
        self.contentLabel.text = @"";
    }
    
    [self setNeedsUpdateConstraints];
}

- (void)updateConstraints
{
    [super updateConstraints];
    
    MediaModel *mediaModel = [self.model mediaModel];
    
    if (mediaModel) {
        // 1.1 有图
        CGFloat ratio = mediaModel.w.floatValue / mediaModel.h.floatValue;
        CGFloat w = 0;
        CGFloat h = 0;
        if (ratio >= 1) {
            w = 120;
            h = w * ratio;
        }
        else {
            h = 120;
            w = h / ratio;
        }
        self.imageView_width_constraint.constant = w;
        self.imageView_height_constraint.constant = h;
        self.imageView_top_constraint.constant = 10.f;
    }
    else {
        self.imageView_top_constraint.constant = 0;
        self.imageView_height_constraint.constant = 0;
    }
    
    if (self.model.timelineContent) {
        self.contentLabel_top_constraint.constant = 20;
    }
    else {
        self.contentLabel_top_constraint.constant = 0;
    }
}

@end
